/*
 * @author : Amissah Gideon<kuulmek@yahoo.com>
 */
var SalesPerItems = {

    selected_row_id:null,

    /* dataTable Object */
    objGrid:null,

    /* Function init
     * @param void
     * @return void
     * @access public
     * */
    init:function () {
        var self = this;

        var cal = Calendar.setup({
            onSelect: function(cal) { cal.hide() },
            showTime: true
        });
        cal.manageFields("start_dt_btn", "start_dt", "%B %d, %Y");
        cal.manageFields("end_dt_btn", "end_dt", "%B %d, %Y");

        /*Sortable Table */
        var oTable = null;
        var dataUrl = $('#table-url').val();

        self.objGrid = $('#flex').flexigrid({
            url:dataUrl,
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                {display:'Item', name:'customer_id', width:270, sortable:true, align:'center', hide:false},
                {display:'Total Unit Price', name : 'unit_price', width : 100, sortable : false, align: 'center', hide:false},
                {display:'Total Discount', name : 'discount', width : 100, sortable : false, align: 'center', hide:false},
                {display:'Total Cost', name : 'cost', width : 100, sortable : false, align: 'center', hide:false},
                {display:'Total Qty', name:'qty', width:60, sortable:true, align:'center', hide:false},
                {display:'Total Sales', name : 'deposit', width : 150, sortable : false, align: 'center', hide:false}
                //{display:'Payment', name:'loan', width:100, sortable:false, align:'center', hide:false},
                //{display:'Outstanding', name:'interest', width:100, sortable:false, align:'center', hide:false}
            ],
            formFields:[
                {type:'buttom', name:'Print', bclass:'print', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Export', bclass:'export', onpress:self.handleGridEvent},
                {separator:true},
                {type:'select', name:'Select Currency', id:'select_currency' ,bclass:'person', options:gbl_currency_opts , onchange:self.handleGridEvent}
                //{separator:true},
                //{type:'text', name:'Start', bclass:'print', id:'start_dt', fclass:'date-picker', default_value:''+$('#start_dt_val').val()+''},
                //{separator:true},
                //{type:'text', name:'End', bclass:'export', id:'end_dt',fclass:'date-picker', default_value:''+$('#end_dt_val').val()+''},
                //{type:'buttom', name:'Reload', bclass:'reload', onpress:self.handleGridEvent}
            ],
            /*searchitems : [
             {display: 'Name', name : 'name', isdefault: true}
             ],*/
            checkboxSelection:false,
            columnControl:true,
            sortname:"id",
            sortorder:"desc",
            //usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            onSubmit: self.addFormData
        });

        self.initDataTypeMenus();

        $('#reload-btn').click(function () {
            var val = $("#select_currency").val();
            self.filterCustomers(val);
        });
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'Print') {
            SalesPerItems.print_export_Details('print');
        }
        else if (com == 'Export') {
            SalesPerItems.print_export_Details('export');
        }
        else if (com == 'Select Currency') {
            SalesPerItems.filterCustomers(json);
        }
    },

    filterCustomers:function(value){
        var self = this;
        var start_dt = $("#start_dt").val();
        var end_dt = $("#end_dt").val();
        self.objGrid.flexOptions({params: [{name:'currency',value:value},{name:'start_dt',value:start_dt},{name:'end_dt',value:end_dt}]}).flexReload();
        //console.log(value);
        return false;
    },

    addFormData:function(){
        var self = this;
        var val = $("#select_currency").val();
        var start_dt = $("#start_dt").val();
        var end_dt = $("#end_dt").val();
        //self.objGrid.flexOptions({params: [{name:'customer_id',value:val}]});
        $('#flex').flexOptions({params: [{name:'currency',value:val},{name:'start_dt',value:start_dt},{name:'end_dt',value:end_dt}]});
        return true;
    },


    print_export_Details: function(data_type){
        var dataUrl = $('#print-export-sales_item-url').val();
        var currency = $("#select_currency").val();
        var start_dt = $("#start_dt").val();
        var end_dt = $("#end_dt").val();

        var url = dataUrl+'/'+currency+'/'+data_type+'/'+start_dt+'/'+end_dt;
        window.open(url,data_type,"menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
    },

    initDataTypeMenus:function(){
        var self = this;

        $("button.data-type-menu").live('click',function(){
            var action = $(this).attr('action');
            var data_type = $(this).attr('data-type');
            var url = $("#"+action+"-url").val()+'/'+self.selected_row_id+'/'+data_type;

            window.open(url,action,"menubar=yes, width=600, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
        });

    }
};

/* when the page is loaded */
$(document).ready(function () {
    SalesPerItems.init();
});